package com.atguigu.day05;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

public class Flink03_TimeWindow_TumblingWindow_AggFun {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据转为WaterSensor
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.将相同id的数据聚合到一块
        KeyedStream<WaterSensor, Tuple> keyedStream = map.keyBy("id");

        // 5.开启一个基于时间的滚动窗口 窗口大小为 5S
        WindowedStream<WaterSensor, Tuple, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

       //TODO 6.使用增量聚合函数AggFun实现一个累加功能
        window.aggregate(new AggregateFunction<WaterSensor, Tuple2<String, Integer>, Tuple2<String, Integer>>() {
            //创建累加器 会为每一个窗口创建一个累加器
            @Override
            public Tuple2<String, Integer> createAccumulator() {
                System.out.println("创建累加器");
                return new Tuple2<>("", 0);
            }

            //累加操作 给累加器重新把累加后的值赋值进去
            @Override
            public Tuple2<String, Integer> add(WaterSensor value, Tuple2<String, Integer> accumulator) {
                System.out.println("累加操作");
                return Tuple2.of(value.getId(), value.getVc() + accumulator.f1);
            }

            //获取结果，将窗口计算的结果返回
            @Override
            public Tuple2<String, Integer> getResult(Tuple2<String, Integer> accumulator) {
                System.out.println("返回结果");
                return accumulator;
            }

            //合并累加器 直接会话窗口合并的时候调用
            @Override
            public Tuple2<String, Integer> merge(Tuple2<String, Integer> a, Tuple2<String, Integer> b) {
                System.out.println("合并累加器");
                return Tuple2.of(a.f0, b.f1 + a.f1);
            }
        }).print();

        env.execute();

    }
}
